﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using WgFrame.Net;

public class CreatePanel : BasePanel
{
    private static string[] randomNames = new string[] { "极镀ギ紬荕","爷霸气傀儡","梦战苍穹","傲世哥","мaη肆風聲","一刀メ隔世",
                                                  "横刀メ绝杀","不死你死你","魔帝殤邪","封刀不再战","倾城孤狼","戎马江湖","狂得像风","影之哀伤","謸氕づ独尊"};

    private static string Text = "大千世界，位面交汇，万族林立，群雄荟萃，一位位来自下位面的天之至尊，在这无尽世界，演绎着令人向往的传奇，追求着那主宰之路。　　" +
        "无尽火域，炎帝执掌，万火焚苍穹。　" +
        "　武境之内，武祖之威，震慑乾坤。　　" +
        "西天之殿，百战之皇，战威无可敌。　　" +
        "北荒之丘，万墓之地，不死之主镇天地。　　......　　" +
        "少年自北灵境而出，骑九幽冥雀，闯向了那精彩绝伦的纷纭世界，主宰之路，谁主沉浮？　　" +
        "大千世界，万道争锋，吾为大";

    private GList listSelectSex;
    private GButton btnRanName;
    private GTextInput inputName;
    private GButton btnOk;
    private GTextField tfText;
    private int idx;

    public override void Init()
    {
        base.Init();
        listSelectSex = this.view.GetChild("n11").asList;
        btnRanName = this.view.GetChild("n10").asButton;
        inputName = this.view.GetChild("n13").asTextInput;
        btnOk = this.view.GetChild("n9").asButton;
        tfText = this.view.GetChild("n2").asTextField;

        listSelectSex.selectedIndex = 0;
        btnRanName.onClick.Add(OnRandomNameClick);
        btnOk.onClick.Add(OnOkClick);
    }

    public override void Show(params object[] para)
    {
        base.Show();
        idx = 0;
        Timers.inst.Add(0.08f, Text.Length+100, OnTextClick);
    }

    public override void Close()
    {
        Timers.inst.Remove(OnTextClick);
        base.Close();
    }

    public override void Dispose()
    {
        Timers.inst.Remove(OnTextClick);
        base.Dispose();
    }

    private void OnTextClick(object param)
    {
        tfText.text += Text[idx++];
        if (idx >= Text.Length)
        {
            Timers.inst.Remove(OnTextClick);
        }
    }

    protected override GComponent CreateView()
    {
        return UIPackage.CreateObject("Main", "panel_create_role").asCom;
    }

    private void CreateRole()
    {
        Dictionary<byte, object> parameters = new Dictionary<byte, object>();
        parameters.Add(1, GameNetwork.ClientGuId);
        parameters.Add(2, inputName.text);
        parameters.Add(3, listSelectSex.selectedIndex == 0);
        GameNetwork.SendOperation((short)OperationCode.CreatePlayer, parameters, new SendOptions());
    }

    private void OnRandomNameClick(EventContext context)
    {
        inputName.text = randomNames[UnityEngine.Random.Range(0, randomNames.Length - 1)];
    }

    private void OnOkClick(EventContext context)
    {
        if (string.IsNullOrEmpty(inputName.text))
        {
            return;
        }
        CreateRole();
    }
}
